﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using T.ServiceComponent.I18N.Service;

namespace T.FAS.Runtime.Base.Entity
{
    /// <summary>
    /// 数据返回集合
    /// </summary>
    public class ActionViewModel
    {
        public string DataResultType { get; set; }
        /// <summary>
        /// 具体的结构描述
        /// </summary>
        public List<ViewModelItem> RecordSchema { get; set; }
        public List<ActionListMethod> ListTypeMethod
        {
            get
            {
                if (DataResultType.ToLower().Equals("record"))
                    return new List<ActionListMethod>();
                else
                {
                    if (_listTypeMethod == null)
                    {
                        _listTypeMethod = GetDefalutListTypeMethod();
                    }
                    return _listTypeMethod;
                }
            }
        }

        private List<ActionListMethod> GetDefalutListTypeMethod()
        {
            var list = new List<ActionListMethod>();
            list.Add(new ActionListMethod()
            {
                Code = "List",
                Description = "List",
                ReturnDataType = DataType.List,
                JSFunc = "#GetList",
                SortOrder = 0,
                SubMethods = new List<ActionListMethod>()
            {new ActionListMethod()
            {Code = "Current", Description = $"{TermService.Current.GetText("T_FAS_fasr_Base_560694594", "获取当前记录")}", ReturnDataType = DataType.Record, JSFunc = "@RecordSchema", SortOrder = 0, }, new ActionListMethod()
            {Code = "CurrentRowNumber", Description = $"{TermService.Current.GetText("T_FAS_fasr_Base_944409275", "获取当前行")}", ReturnDataType = DataType.Int, JSFunc = "#GetCurrentRowNumber", SortOrder = 1, }, new ActionListMethod()
            {Code = "Rows", Description = $"{TermService.Current.GetText("T_FAS_fasr_Base_1225880092", "获取数据行数")}", ReturnDataType = DataType.Int, JSFunc = "#GetRows", SortOrder = 2, }, new ActionListMethod()
            {Code = "Empty", Description = $"{TermService.Current.GetText("T_FAS_fasr_Base_1729006105", "数据是否为空")}", ReturnDataType = DataType.Boolean, JSFunc = "#IsEmpty", SortOrder = 3, }}
            });
            list.Add(new ActionListMethod()
            { Code = "Count", Description = $"{TermService.Current.GetText("T_FAS_fasr_Base_1549850694", "获取数据总条数")}", ReturnDataType = DataType.Int, JSFunc = "#GetCount", SortOrder = 1, });
            return list;
        }

        public ActionViewModel(string mainViewModelItemCode)
        {
            _mainViewModelItemCode = mainViewModelItemCode;
        }
        [JsonIgnore]

        private string _mainViewModelItemCode = string.Empty;
        [JsonIgnore]
        public string MainViewModelItemCode
        {
            get
            {
                if (string.IsNullOrEmpty(_mainViewModelItemCode) && RecordSchema != null && RecordSchema.Count > 0)
                {
                    _mainViewModelItemCode = RecordSchema[0].ViewModelItemCode;
                }
                return _mainViewModelItemCode;
            }
            set { _mainViewModelItemCode = value; }
        }
        [JsonIgnore]
        public ViewModelItem MainViewModelItem
        {
            get
            {
                if (RecordSchema != null && RecordSchema.Count > 0)
                {
                    return RecordSchema.Find(x => x.ViewModelItemCode.Equals(MainViewModelItemCode));
                }
                return new ViewModelItem();
            }
        }
        /// <summary>
        /// 数据结果返回类型 Record或者List
        /// </summary>
        [JsonIgnore]

        private List<ActionListMethod> _listTypeMethod = null;
    }
}
